/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.editor.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * Figure that represents a package. It can contain other figures such as
 * classes, interfaces or other packages
 */
public class PackageFigure extends Figure {

	/**
	 * Label containing the name of the package
	 */
	private EditableLabel nameLabel;

	/**
	 * Figure to create space around the mainFig.
	 */
	@SuppressWarnings("unused")
	private Figure outerMainFig;

	/**
	 * Contains all figures but the name label (classes, packages, etc).
	 */
	private Figure mainFig;

	/**
	 * Constructor for a new PackageFigure.
	 * 
	 * @param name
	 *            The name of the package.
	 */
	public PackageFigure(EditableLabel name) {
		super();

		setBorder(null);
		// vertical
		setLayoutManager(new ToolbarLayout());

		// create helper figure to achieve stretching of main figure without
		// stretching of label
		Figure upperFig = new Figure();
		ToolbarLayout upperLayout = new ToolbarLayout();
		upperLayout.setStretchMinorAxis(false);
		upperFig.setLayoutManager(upperLayout);
		nameLabel = name;
		nameLabel.setBorder(new LineBorder(ColorConstants.black, 1));
		nameLabel.setLabelAlignment(PositionConstants.LEFT);
		upperFig.setBorder(null);
		upperFig.add(nameLabel);
		upperFig.setOpaque(true);

		add(upperFig);

		setBorder(new PackageBorder(upperFig));

		// create figure that will contain all classes, subpackages etc.
		mainFig = new Figure();
		// mainFig.setBorder(new LineBorder(ColorConstants.black, 1));
		mainFig.setLayoutManager(new XYLayout());
		mainFig.setOpaque(true);
		add(mainFig);

	}

	/**
	 * Get the figure in which children figures (as classes or inner packages)
	 * should be displayed.
	 * 
	 * @return the main figure containing all children figures
	 */
	public Figure getMainFigure() {
		return mainFig;
	}

	/**
	 * Border around the package. Paints a rectangular border that starts under
	 * the upperFigure.
	 * 
	 * @author Michael Pradel
	 * 
	 */
	private class PackageBorder extends MarginBorder {

		private Figure upperFigure;

		/**
		 * Constructor of a new PackageBorder.
		 * 
		 * @param newUpperFigure
		 *            the figure on top of the border
		 */
		public PackageBorder(final Figure newUpperFigure) {
			// call constructor of MarginBorder
			super(0, 0, 0, 0);
			// set upper Figure
			this.upperFigure = newUpperFigure;
		}

		/**
		 * Paint or repaint the PackageBorder.
		 * 
		 * @param figure
		 *            the figure to receive the PackageBorder
		 * @param graphics
		 *            the graphics to draw on
		 * @param insets
		 *            the insets
		 */
		@Override
		public void paint(final IFigure figure, final Graphics graphics,
				final Insets insets) {
			Rectangle r = getPaintRectangle(figure, insets);
			int y = r.y + upperFigure.getSize().height;
			int height = r.height - upperFigure.getSize().height;
			// upper line
			graphics.drawLine(r.x, y, r.x + r.width, y);
			// left line
			graphics.drawLine(r.x, y, r.x, y + height - 1);
			// right line
			graphics.drawLine(r.x + r.width - 1, y, r.x + r.width - 1, y
					+ height - 1);
			// bottom line
			graphics.drawLine(r.x, y + height - 1, r.x + r.width, y + height
					- 1);

		}

	}

	/**
	 * @return Returns the nameLabel.
	 */
	public EditableLabel getNameLabel() {
		return nameLabel;
	}

}
